#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
void MainWindow::Sleep(int msec)
{
    QTime dieTime = QTime::currentTime().addMSecs(msec);
    while( QTime::currentTime() < dieTime )
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    tcpSocket = new QTcpSocket(this);
    tcpServer = new QTcpServer(this);
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete tcpSocket;
    delete tcpServer;
    delete ui;
}


void MainWindow::on_creatClient_clicked()
{
    if(ui->creatClient->text()==tr("创建客户端"))
    {
        if(ui->serverIP->text()==tr("")||ui->port->text()==tr(""))  //未输入
        {
            QMessageBox::warning(this, tr("Waring"),tr("未输入端口号/IP号"),QMessageBox::Yes);
        }
        else
        {

            connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessage()));
            tcpSocket->abort();
            //连接到主机，这里从界面获取主机地址和端口号
            tcpSocket->connectToHost(ui->serverIP->text(),ui->port->text().toInt());
            Sleep(1000);
            if(tcpSocket->state()!=QTcpSocket::ConnectedState)
            {
                QMessageBox::warning(this, tr("Waring"),tr("连不上兄弟"),QMessageBox::Yes);
                return;
            }
            ui->creatClient->setText(tr("关闭客户端"));
            ui->creatServer->hide();


        }
    }
    else
    {
        tcpSocket->disconnectFromHost();
        ui->creatClient->setText(tr("创建客户端"));
        ui->creatServer->show();
    }
}
void MainWindow::readMessage()
{
    QByteArray array = tcpSocket->readAll();

    //将接收到的数据存放到变量中
    ui->RECEIEVE->append(array);
}


void MainWindow::on_send_clicked()
{
    if(ui->creatClient->text()==tr("创建客户端")&&ui->creatClient->text()==tr("创建服务器"))
    {
        QMessageBox::warning(this, tr("Waring"),tr("未连接"),QMessageBox::Yes);
        return;
    }

    QString tmpStr;

    tmpStr+=ui->SendTexty->toPlainText();
    if(tmpStr==tr(""))  //未输入
    {
        return ;
    }

    tcpSocket->write(tmpStr.toUtf8(),tmpStr.size());
}

void MainWindow::on_creatServer_clicked()
{
    if(ui->creatServer->text()==tr("创建服务器"))
    {
        if(ui->port->text()==tr(""))  //未输入
        {
            QMessageBox::warning(this, tr("Waring"),tr("未输入端口号"),QMessageBox::Yes);
        }
        else
        {

            if(!tcpServer->listen(QHostAddress::Any,ui->port->text().toInt()))
            {
                qDebug() << tcpServer->errorString();
                close();
            }
            connect(tcpServer,SIGNAL(newConnection()),this,SLOT(connectSuccess()));
            ui->creatServer->setText(tr("关闭服务器"));
            ui->creatClient->hide();
        }
    }
    else
    {
        tcpServer->disconnect();
        tcpServer->close();
        tcpServer->close();
        tcpServer->close();
        ui->creatServer->setText(tr("创建服务器"));
        ui->creatClient->show();
    }
}
void MainWindow::connectSuccess()
{
    tcpSocket = tcpServer->nextPendingConnection();
    connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessage()));
}

void MainWindow::on_carForward1_clicked()
{
    QString tmpStr;
    tmpStr="carf1";
    tcpSocket->write(tmpStr.toUtf8(),tmpStr.size());
}

void MainWindow::on_carForward2_clicked()
{
    QString tmpStr;
    tmpStr="carf2";
    tcpSocket->write(tmpStr.toUtf8(),tmpStr.size());
}

void MainWindow::on_carForward3_clicked()
{
    QString tmpStr;
    tmpStr="carf3";
    tcpSocket->write(tmpStr.toUtf8(),tmpStr.size());
}

void MainWindow::on_clearReceieve_clicked()
{
    ui->RECEIEVE->clear();
}
